var postsData = require('../../../data/posts-data.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collected: false,
    isPlayMusic:false,
    postData:{},
    animation: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var postId = options.id
    var that = this;
    this.data.currentPostId = postId
    this.data.postData = postsData.postList[postId];
    // this.data.postData = postData
    // console.log(this.data.postData)
    this.setData({
      postData: postsData.postList[postId]
    });
    // wx.setStorageSync('kesy', 'g断干净')
    // wx.setStorageSync('key', "开发中")
    // wx.setStorageSync('sdfkey', "开发中")
    var postsCollectedObj ={};
    if (wx.getStorageSync("posts_collected")!= ""){
      postsCollectedObj = wx.getStorageSync("posts_collected")
    }
    if (postsCollectedObj[postId]){
      var postCollectedBl = postsCollectedObj[postId]
      this.setData({
        collected: postCollectedBl
      })
    }else{
      postsCollectedObj[postId] = false;
      wx.setStorageSync("posts_collected", postsCollectedObj)
    }
    wx.onBackgroundAudioPlay(function(){
      that.setData({
        isPlayMusic: true
      })
      that.onPlayMusic();
    })
    wx.onBackgroundAudioPause(function () {
      that.setData({
        isPlayMusic: false
      })
      that.onPlayMusic();
    })
   
  },
  onColletionTap:function(event){
    //同步调用
    that.getPostsCollectedSyc();
    //异步调用
    //this.getPostsCollectedAsy();

  },
  getPostsCollectedAsy:function(){
    var that = this;
    wx.getStorage({ 
      key:"posts_collected",
      success:function(res){
        var postsCollectedObj = res.data
        var postCollectedBl = postsCollectedObj[that.data.currentPostId]
        //收藏状态变化设置
        postCollectedBl = !postCollectedBl
        postsCollectedObj[that.data.currentPostId] = postCollectedBl
        that.showModal(postCollectedBl, postsCollectedObj);
      }
    })
    
  },
  getPostsCollectedSyc:function(){
    var postsCollectedObj = wx.getStorageSync("posts_collected")
    var postCollectedBl = postsCollectedObj[this.data.currentPostId]
    //收藏状态变化设置
    postCollectedBl = !postCollectedBl
    postsCollectedObj[this.data.currentPostId] = postCollectedBl
    this.showModal(postCollectedBl, postsCollectedObj);
  },
  showModal: function (postCollectedBl, postsCollectedObj){
    var that = this;
    wx.showModal({
      title: '收藏',
      content: postCollectedBl?'收藏该文章':'取消收藏该文章',
      cancelText: "取消",
      cancelColor: "#000",
      confirmText: "确认",
      confirmColor: '#009933',
      success: function (res) {
        if (res.confirm) {
          that.showToast(postCollectedBl, postsCollectedObj)
        } else if (res.cancel) {

        }
      }, fail: function () {

      }, complete: function () {

      }
    })
  },
  showToast: function (postCollectedBl, postsCollectedObj){
    //更新文章是否的缓存值
    wx.setStorageSync("posts_collected", postsCollectedObj)
    //更新数据绑定变量，从而实现切换图片
    this.setData({
      collected: postCollectedBl
    })
    wx.showToast({
      title: postCollectedBl ? '收藏成功' : "取消成功",
      duration: 1000,
      icon: 'success'
    })
  },
  onShareTyp:function(){
    var arrItem = ["分享到微信好友", "分享到朋友圈", "分享到QQ", "分享到微博"]
    wx.showActionSheet({
      itemList: arrItem,
      itemColor:"#405f80",
      success: function (res) {
        wx.showModal({
          title: '用户' + arrItem[res.tapIndex],
          content: '用户是否取消？' + res.cancel +"不支持分享功能"
        })
      },
      fail: function (res) {
        wx.showModal({
          title: '用户' + arrItem[res.tapIndex],
          content: res.errMsg
        })
      }
    })
  },
  onMusicTap: function () {
    var isPlayMusic = this.data.isPlayMusic
    if (isPlayMusic){
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayMusic: false
      })
    }else{
      wx.playBackgroundAudio({
        dataUrl: this.data.postData.music.url,
        title: this.data.postData.music.title,
        coverImgUrl: this.data.postData.music.coverImg
      })
      this.setData({
        isPlayMusic: true
      })
    }
    this.onPlayMusic();
  },
  onPlayMusic:function(){
    var thisParent = this;
    var timerObj = 0;
    var n = 0;
    this.animation = wx.createAnimation({
      duration: 1400,
      timingFunction: 'linear',
      delay: 0,
      transformOrigin: '50% 50% 0'
    })
    timerObj = setInterval(function () {
      n++;
      thisParent.animation.rotate(180 * (n)).step()
      thisParent.setData({
        animation: thisParent.animation.export()
      })
      if (!thisParent.data.isPlayMusic) {
        n = 0;
        clearInterval(timerObj);
        timerObj = null;
      }
    }, 1000)
  }
  ,
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})